package com.hongyi.controller;

import com.hongyi.common.Result;
import com.hongyi.pojo.User;
import com.hongyi.serivice.UserService;

import com.hongyi.vo.UserVo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hongyi
 * @date 2023/11/22 21:16
 */
@RestController
@RequestMapping("/user")
@MapperScan("com.hongyi.mapper")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/{id}") // /user/1
    public Result<UserVo> getById(@PathVariable Long id) {
        UserVo userVo = userService.getById(id);
        return Result.success(userVo);
    }

    /**
     * application/json
     * {
     * "name": "列好战共",
     * "age": 83,
     * "email": "r.bwjryki@qq.com"
     * }
     *
     * @param user
     * @return
     */
    // 新增
    @PostMapping("/insert") // /insert /insert application/json
    public Result<User> addUser(@RequestBody User user) {
        userService.save(user);
        return Result.success();
    }

    // 更新
    @PutMapping("/update") // /update /update application/json
    public Result<User> updateUser(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    // 删除
    @DeleteMapping("/delete/{id}") // /detelete/1   http://127.0.0.1:9093/user/delete/1
    public Result<User> deleteUser(@PathVariable Long id) {
        userService.delete(id);
        return Result.success();
    }

    // 查询 所有
    @GetMapping("/findAll") // /findAll http://127.0.0.1:9093/user/findByName3?name=A
    public Result<List<UserVo>> findAll() {
        List<UserVo> userVo = userService.findALl();
        return Result.success(userVo);
    }

    // 根据姓名查询
    @GetMapping("/findByName2") // /findByName?name=张三
    public Result<List<UserVo>> findByName(String name) {
        List<UserVo> userVos = userService.findName(name);
        return Result.success(userVos);
    }


    // 根据姓名查询
    @GetMapping("/findByUserVo") // /findByName?name=张三
    public Result<List<UserVo>> findByName(User user) {
        List<UserVo> userVos = userService.findUser(user);
        return Result.success(userVos);
    }


   /* @GetMapping("/findByName")
    public Result<List<UserVo>> findByName(RequestParam String name) {
       userService.findName(name);
        return Result.success(userVos);
    }*/


}
// 127.0.0.0.1:9091/user/user/{id}
